local zongkui = fk.CreateSkill {
  name = "rmt__zongkui"
}

Fk:loadTranslationTable{
  ["rmt__zongkui"] = "纵傀",
  [":rmt__zongkui"] = "回合开始后，你可指定一名没有“傀”的其他角色，令其获得1枚“傀”。每轮开始时，体力值最小且没有“傀”的一名其他角色获得1枚“傀”。",

  ["@@rmt__puppet"] = "傀",
  ["#rmt__zongkui-ask"] = "纵傀：选择一名其他角色，令其获得一枚“傀”",

  ["$rmt__zongkui1"] = "不要抵抗，接受我的操纵吧。",
  ["$rmt__zongkui2"] = "当我的傀儡，你将受益良多。",
}

zongkui:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(zongkui.name) then
      return false
    end

    local min = 999
    local availableTargets = {}
    table.forEach(player.room:getOtherPlayers(player, false), function(p)
      if p.hp <= min then
        if p.hp < min then
          availableTargets = {}
          min = p.hp
        end
        table.insert(availableTargets, p)
      end
    end)

    return table.find(availableTargets, function(p) return p:getMark("@@rmt__puppet") == 0 end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local min = 999
    local availableTargets = {}
    table.forEach(player.room:getOtherPlayers(player, false), function(p)
      if p.hp <= min then
        if p.hp < min then
          availableTargets = {}
          min = p.hp
        end
        table.insert(availableTargets, p)
      end
    end)
    availableTargets = table.filter(availableTargets, function(p) return p:getMark("@@rmt__puppet") == 0 end)
    if #availableTargets == 0 then
      return false
    end

    local tos = room:askToChoosePlayers(
      player,
      {
        targets = availableTargets,
        min_num = 1,
        max_num = 1,
        prompt = "#rmt__zongkui-ask",
        skill_name = zongkui.name,
        cancelable = false,
      }
    )
    event:setCostData(self, tos[1])
    return true
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(event:getCostData(self), "@@rmt__puppet")
  end,
})

zongkui:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(zongkui.name) and
      table.find(player.room.alive_players, function(p)
        return p:getMark("@@rmt__puppet") == 0 and p ~= player
      end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p)
      return p:getMark("@@rmt__puppet") == 0 and p ~= player
    end)
    local tos = room:askToChoosePlayers(
      player,
      {
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#rmt__zongkui-ask",
        skill_name = zongkui.name,
      }
    )
    if #tos > 0 then
      event:setCostData(self, tos[1])
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    player.room:addPlayerMark(event:getCostData(self), "@@rmt__puppet")
  end,
})

return zongkui
